/*
 * Created by JFormDesigner on Fri Nov 01 16:15:24 CST 2024
 */

package com.hafu.view.GYS;

import com.hafu.model.GysInfo;
import com.hafu.service.GysInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author 25188
 */
public class ManageGYSFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();
    public ManageGYSFrm() {
        initComponents();
        fillTable();
    }

    private void fillTable() {
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoName(new GysInfo());
        //获取表格模型
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        for(GysInfo gysInfo:gysInfoList){
            Vector<Object> rowData = new Vector<>();
            rowData.add(gysInfo.getId());
            rowData.add(gysInfo.getName());
            rowData.add(gysInfo.getTel());
            //添加一行数据
            model.addRow(rowData);
        }
    }


    private void queryGYSbtn(ActionEvent e) {
        // TODO add your code here  根据名字查询供应商信息事件
        String name = textField1.getText();
        GysInfo gysInfo = new GysInfo();
        gysInfo.setName(name);
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoName(gysInfo);
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        if (gysInfoList.size() > 0){
            for(GysInfo gysInfo1 : gysInfoList){
                Vector<Object> rowData = new Vector<>();
                rowData.add(gysInfo1.getId());
                rowData.add(gysInfo1.getName());
                rowData.add(gysInfo1.getTel());
                //添加一行数据
                model.addRow(rowData);
            }
        }else {
            JOptionPane.showMessageDialog(null,"没有找到该供应商信息");
        }
    }


    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here  获取选中行
        int row = table1.getSelectedRow();
        //得到行的数据
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        int chooseId = (int) model.getValueAt(row, 0);
        String name = (String) model.getValueAt(row, 1);
        String tel = (String) model.getValueAt(row, 2);
        textField2.setText(name);
        textField3.setText(tel);
    }

    private void scrollPane1MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void updateGYSbtn(ActionEvent e) {
        // TODO add your code here  修改供应商信息事件
        String name = textField2.getText();
        String tel = textField3.getText();
        if (StringUtil.isEmpty(name) && StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null,"请选择要修改的供应商");
            return;
        }
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"供应商名称不能为空");
            return;
        }
        if (StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null,"供应商电话不能为空");
            return;
        }
        if(tel.length() != 11){
            JOptionPane.showMessageDialog(null, "供应商电话格式不正确");
            return;
        }
        int row = table1.getSelectedRow();
        //得到行的数据
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        int chooseId = (int) model.getValueAt(row, 0);
        GysInfo gysInfo = new GysInfo(chooseId, tel, name);
        Boolean aBoolean = gysInfoService.updateGysInfo(gysInfo);
        if (aBoolean){
            JOptionPane.showMessageDialog(null, "修改成功");
            fillTable();
        }else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }

    private void deleteGYSbtn(ActionEvent e) {
        // TODO add your code here  删除供应商信息事件
        int row = table1.getSelectedRow();
        if (row == -1){
            JOptionPane.showMessageDialog(null, "请选择要删除的供应商！");
            return;
        }
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        int chooseId = (int) model.getValueAt(row, 0);
        Boolean aBoolean = gysInfoService.deleteGysInfo(chooseId);
        if (aBoolean){
            JOptionPane.showMessageDialog(null, "删除成功");
            fillTable();
            textField2.setText("");
            textField3.setText("");
        }else {
            JOptionPane.showMessageDialog(null, "删除失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField1 = new JTextField();
        updateGYSbtn = new JButton();
        deleteGYSbtn = new JButton();
        textField3 = new JTextField();
        queryGYSbtn = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u4f9b\u5e94\u5546");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u4f9b\u5e94\u5546\u540d\u5b57");

        //======== scrollPane1 ========
        {
            scrollPane1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    scrollPane1MouseClicked(e);
                }
            });

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u4f9b\u5e94\u5546\u540d\u5b57");

        //---- label3 ----
        label3.setText("\u4f9b\u5e94\u5546\u7535\u8bdd");

        //---- updateGYSbtn ----
        updateGYSbtn.setText("\u4fee\u6539");
        updateGYSbtn.addActionListener(e -> updateGYSbtn(e));

        //---- deleteGYSbtn ----
        deleteGYSbtn.setText("\u5220\u9664");
        deleteGYSbtn.addActionListener(e -> deleteGYSbtn(e));

        //---- queryGYSbtn ----
        queryGYSbtn.setText("\u67e5\u8be2");
        queryGYSbtn.addActionListener(e -> queryGYSbtn(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(103, 103, 103)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(label2)
                                .addComponent(label3))
                            .addGap(18, 18, 18)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                            .addGap(40, 40, 40)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(updateGYSbtn)
                                .addComponent(deleteGYSbtn)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(75, 75, 75)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label1)
                                    .addGap(18, 18, 18)
                                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 177, GroupLayout.PREFERRED_SIZE)
                                    .addGap(55, 55, 55)
                                    .addComponent(queryGYSbtn)
                                    .addGap(35, 35, 35))
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(34, 34, 34)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1)
                        .addComponent(queryGYSbtn))
                    .addGap(36, 36, 36)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
                    .addGap(13, 13, 13)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(updateGYSbtn))
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(23, 23, 23)
                            .addComponent(label3))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(deleteGYSbtn)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
                    .addContainerGap(52, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField1;
    private JButton updateGYSbtn;
    private JButton deleteGYSbtn;
    private JTextField textField3;
    private JButton queryGYSbtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
